package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.UserDesignDetailDAO;
import com.daxia.ogmall.model.UserDesignDetail;
import com.daxia.ogmall.dto.UserDesignDetailDTO;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class UserDesignDetailService {
	
	@Autowired
	private UserDesignDetailDAO userDesignDetailDAO;
	
	public List<UserDesignDetailDTO> find(UserDesignDetailDTO query, Page page) {
		List<UserDesignDetail> models = userDesignDetailDAO.find(query, page);
		List<UserDesignDetailDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<UserDesignDetailDTO> findAll() {
		return this.find(new UserDesignDetailDTO(), null);
	}
	
	public List<UserDesignDetailDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new UserDesignDetailDTO(), page);
	}
	
	public Long create(UserDesignDetailDTO dto) {
		UserDesignDetail model = new UserDesignDetail();
		toModel(model, dto);
		return userDesignDetailDAO.create(model);
	}
	
	public UserDesignDetailDTO load(Long id) {
	    UserDesignDetail model = userDesignDetailDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(UserDesignDetailDTO dto) {
		UserDesignDetail model = userDesignDetailDAO.load(dto.getId());
		toModel(model, dto);
		userDesignDetailDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				throw new RuntimeException("未实现删除逻辑，请确认是物理删除还是逻辑删除");
				// userDesignDetailDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public UserDesignDetailDTO findOne(UserDesignDetailDTO query) {
		UserDesignDetail model = userDesignDetailDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<UserDesignDetailDTO> toDTOs(List<UserDesignDetail> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<UserDesignDetailDTO>(0);
		}
		List<UserDesignDetailDTO> dtos = new ArrayList<UserDesignDetailDTO>(models.size());
		for (UserDesignDetail model : models) {
	        UserDesignDetailDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private UserDesignDetailDTO toDTO(UserDesignDetail model) {
		if (model == null) {
			return null;
		}
		UserDesignDetailDTO dto = BeanMapper.map(model, UserDesignDetailDTO.class);
		
		return dto;
	}
	
	private void toModel(UserDesignDetail model, UserDesignDetailDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<UserDesignDetail> toModels(List<UserDesignDetailDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<UserDesignDetail>(0);
		}
		List<UserDesignDetail> models = new ArrayList<UserDesignDetail>(dtos.size());
		for (UserDesignDetailDTO dto : dtos) {
	        UserDesignDetail model = new UserDesignDetail();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
